<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col>
        <el-button type="primary" plain @click="addProject"
          >添加考试项目</el-button
        >
      </el-col>
    </el-row>

    <el-form ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="项目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="考试名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--    表格数据-->
    <el-table
      v-loading="loading"
      :data="tableData"
      @selection-change="handleSelectionChange"
      ref="tableRef"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
        :selectable="selectable"
      />
      <el-table-column label="考试名称" prop="projectName" min-width="200" />
      <el-table-column label="项目费用（元）" prop="cost" min-width="50" />
      <el-table-column label="是否为必报科目" prop="isNecessary" min-width="50">
        <template #default="scope">
          <el-space v-if="scope.row.isNecessary == 0" type="text">否</el-space>
          <el-space v-if="scope.row.isNecessary == 1" type="text">是</el-space>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { getCurrentInstance, onMounted } from "@vue/runtime-core";
import { toRefs } from "@vueuse/shared";
import { getProjectList, addProjects } from "../../../../../api/train/verity";
import { useRouter } from "vue-router";
import { useRoute } from "vue-router";
const route = useRoute();
const router = useRouter();

const tableData = ref([]);
const total = ref(0);
const loading = ref(true);
const { proxy } = getCurrentInstance();
const trainId = route.params.trainId;
let selectedIds = [];
const tableRef = ref();
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    trainId: trainId,
    name: undefined,
    flag: 1,
  },
  rules: {},
});
const { queryParams, form, rules } = toRefs(data);

// 获取列表
function getList() {
  loading.value = true;
  getProjectList(queryParams.value).then((res) => {
    loading.value = false;
    tableData.value = res.rows;
    total.value = res.total;
    toggleSelection();
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleSelectionChange(selection) {
  selectedIds = selection.map((item) => item.id);
}

//添加项目
function addProject() {
  let param = {
    ids: selectedIds,
    userId: route.params.userId,
    trainId: trainId,
    flag: 1,
  };
  var title = `是否选择添加 ${selectedIds.length}个非必须考试项目？`;
  proxy.$modal
    .confirm(title)
    .then(function () {
      return addProjects(param);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("添加成功！");
    });
}

// 默认选中
function toggleSelection() {
  nextTick(() => {
    tableData.value.forEach((row) => {
      tableRef.value.toggleRowSelection(row, row.isNecessary == 1);
    });
  });
}
function selectable(row, index) {
  if (row.isNecessary == 1) {
    return false;
  } else {
    return true;
  }
}

onMounted(() => {
  getList();
});
</script>

<style scoped></style>
